<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>User Registration</title>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<body>
<h2>User Registration</h2>
<form id="registerForm">
    <label for="username">Username:</label>
    <input type="text" id="username" name="username"><br><br>

    <label for="password">Password:</label>
    <input type="password" id="password" name="password"><br><br>

    <button type="submit">Register</button>
</form>

<p id="responseMessage"></p>

<script>
    document.getElementById('registerForm').addEventListener('submit', function(event) {
        event.preventDefault(); // 阻止表单默认提交行为

        const username = document.getElementById('username').value;
        const password = document.getElementById('password').value;

        axios.post('/v1/register', {
            username: username,
            password: password
        })
        .then(function(response) {
            document.getElementById('responseMessage').innerText = response.data;
            if (response.data === "Registration successful") {
                // 注册成功后跳转到登录页面
                window.location.href = 'login.html';
            }
        })
        .catch(function(error) {
            document.getElementById('responseMessage').innerText = 'Error: ' + (error.response?.data || error.message);
        });
    });
</script>
</body>
</html>
